﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Reflection;

using ESRI.ArcGIS.Client.Geometry;

using TouchToolkit.Framework;
using TouchToolkit.Framework.TouchInputProviders;
using TouchToolkit.GestureProcessor.Feedbacks.TouchFeedbacks;
using TouchToolkit.GestureProcessor.ReturnTypes;
using TouchToolkit.GestureProcessor.Gesture_Definitions;

namespace eGrid_App
{
    /// <summary>
    /// Interaction logic for My_Application.xaml
    /// Description: This is the Main window. It includes a ScatterLayer and 
    /// 4 corner buttons containing the function menu ("Exit", "List", "Find").
    /// </summary>
    public partial class My_Application : UserControl
    {
        #region members

        public CornerMenu FunctionMenuUpperLeft;
        public CornerMenu FunctionMenuBottomLeft;
        public CornerMenu FunctionMenuUpperRight;
        public CornerMenu FunctionMenuBottomRight;

        private ExitDialog ExitDialogUpperLeft;
        private ExitDialog ExitDialogUpperRight;
        private ExitDialog ExitDialogBottomLeft;
        private ExitDialog ExitDialogBottomRight;

        private ScatterLayer BackgroundMapLayer;

        private Window mainWindow;

        private TouchInputProvider touchProvider;
        #endregion

        #region Constructor and Initializations
        
        public My_Application(TouchInputProvider provider, Window startupWindow)
        {
            InitializeComponent();

            LayoutRoot.Height = System.Windows.SystemParameters.VirtualScreenHeight;
            LayoutRoot.Width = System.Windows.SystemParameters.VirtualScreenWidth;

            mainWindow = startupWindow;
            touchProvider = provider;

            this.Loaded += new RoutedEventHandler(My_Application_Loaded);
        }

        public TouchInputProvider getTouchInputProvider()
        {
            return touchProvider;
        }

        private void My_Application_Loaded(object sender, RoutedEventArgs e)
        {
            GestureFramework.Initialize(touchProvider, LayoutRoot, Assembly.GetExecutingAssembly());
            //GestureFramework.ShowDebugPanel(GestureFramework.DebugPanels.GestureRecorder);
            GestureFramework.AddTouchFeedback(typeof(BubblesPath));
            //This feedback allows the user to see which area has been selected by the gesture
            //GestureFramework.AddGesturFeedback(Gestures.Lasso, typeof(TouchToolkit.GestureProcessor.Feedbacks.GestureFeedbacks.HighlightSelectedArea));
            GestureFramework.EventManager.BubbleUpUnhandledEvents = true;

            BackgroundMapLayer = new ScatterLayer(touchProvider, LayoutRoot.Width, LayoutRoot.Height);
            //ArcGisMap map = new ArcGisMap();
            //map.InitialExtent = new Envelope(-114.25, 51.00, -113.88, 51.05);
            //map.Width = LayoutRoot.Width;
            //map.Height = LayoutRoot.Height;

            //LayoutRoot.Children.Add(map);
            LayoutRoot.Children.Add(BackgroundMapLayer);
           
            CreateCornerMenus();

            InitializeExitDialogs();
        }

        public Window getMainWindow()
        {
            return mainWindow;
        }

        private void CreateCornerMenus()
        {
            var offset = -150;

            // Bottom Left Corner menu
            FunctionMenuBottomLeft = new CornerMenu(this, CornerMenu.CornerMenuLocation.BottomLeft);
            LayoutRoot.Children.Add(FunctionMenuBottomLeft);
            Canvas.SetLeft(FunctionMenuBottomLeft, offset);
            Canvas.SetBottom(FunctionMenuBottomLeft, offset);

            FunctionMenuBottomLeft.LayoutTransform = new RotateTransform(90);

            // Upper Right Corner menu
            FunctionMenuUpperRight = new CornerMenu(this, CornerMenu.CornerMenuLocation.TopRight);
            LayoutRoot.Children.Add(FunctionMenuUpperRight);
            Canvas.SetTop(FunctionMenuUpperRight, offset);
            Canvas.SetRight(FunctionMenuUpperRight, offset);

            FunctionMenuUpperRight.LayoutTransform = new RotateTransform(-90);

            // Upper Left Corner menu
            FunctionMenuUpperLeft = new CornerMenu(this, CornerMenu.CornerMenuLocation.TopLeft);
            LayoutRoot.Children.Add(FunctionMenuUpperLeft);
            Canvas.SetTop(FunctionMenuUpperLeft, offset);
            Canvas.SetLeft(FunctionMenuUpperLeft, offset);
            
            FunctionMenuUpperLeft.LayoutTransform = new RotateTransform(180);

            // Bottom Right Corner menu
            FunctionMenuBottomRight = new CornerMenu(this, CornerMenu.CornerMenuLocation.BottomRight);
            LayoutRoot.Children.Add(FunctionMenuBottomRight);
            Canvas.SetBottom(FunctionMenuBottomRight, offset);
            Canvas.SetRight(FunctionMenuBottomRight, offset);

        }
        
        /// <summary>
        /// Create the exit confirmation dialogs
        /// </summary>
        private void InitializeExitDialogs()
        {
            var offset = 0.0;
            ExitDialogUpperLeft = new ExitDialog(this, "ExitDialogUpperLeft");
            ExitDialogUpperLeft.Visibility = Visibility.Hidden;
            Canvas.SetLeft(ExitDialogUpperLeft, offset);
            Canvas.SetTop(ExitDialogUpperLeft, offset);
            ExitDialogUpperLeft.LayoutTransform = new RotateTransform(180);

            ExitDialogUpperRight = new ExitDialog(this, "ExitDialogUpperRight");
            ExitDialogUpperRight.Visibility = Visibility.Hidden;
            Canvas.SetRight(ExitDialogUpperRight, offset);
            Canvas.SetTop(ExitDialogUpperRight, offset);
            ExitDialogUpperRight.LayoutTransform = new RotateTransform(-90);

            ExitDialogBottomLeft = new ExitDialog(this, "ExitDialogBottomLeft");
            ExitDialogBottomLeft.Visibility = Visibility.Hidden;
            Canvas.SetLeft(ExitDialogBottomLeft, offset);
            Canvas.SetBottom(ExitDialogBottomLeft, offset);
            ExitDialogBottomLeft.LayoutTransform = new RotateTransform(90);

            ExitDialogBottomRight = new ExitDialog(this, "ExitDialogBottomRight");
            ExitDialogBottomRight.Visibility = Visibility.Hidden;
            Canvas.SetRight(ExitDialogBottomRight, offset);
            Canvas.SetBottom(ExitDialogBottomRight, offset);

            LayoutRoot.Children.Add(ExitDialogUpperLeft);
            LayoutRoot.Children.Add(ExitDialogUpperRight);
            LayoutRoot.Children.Add(ExitDialogBottomLeft);
            LayoutRoot.Children.Add(ExitDialogBottomRight);
        }

        #endregion

        #region Corner Button Functions

        /// <summary>
        /// returns whether any of the exit dialogs are visible
        /// </summary>
        /// <returns></returns>
        public bool isExitDialogVisible()
        {
            if (ExitDialogBottomLeft.Visibility == Visibility.Visible ||
                ExitDialogBottomRight.Visibility == Visibility.Visible ||
                ExitDialogUpperLeft.Visibility == Visibility.Visible ||
                ExitDialogUpperRight.Visibility == Visibility.Visible)
                return true;
            return false;
        }

        public void viewSearchDialog()
        {
            BackgroundMapLayer.AddSearchBox(new Point(500, 500));
        }


        /// This brings out a BookmarkListFrame with the same orientation
        /// of the touch finger.  The location of the list is close to the
        /// activating corner function menu.        
        public void showListBox(double orientation, CornerMenu.CornerMenuLocation location)
        {
            switch (location)
            {
                case CornerMenu.CornerMenuLocation.TopLeft:
                    BackgroundMapLayer.AddBookmarkListFrame(new Point(200, 850), orientation);
                    break;
                case CornerMenu.CornerMenuLocation.TopRight:
                    BackgroundMapLayer.AddBookmarkListFrame(new Point(200, 200), orientation);
                    break;
                case CornerMenu.CornerMenuLocation.BottomLeft:
                    BackgroundMapLayer.AddBookmarkListFrame(new Point(550, 850), orientation);
                    break;
                case CornerMenu.CornerMenuLocation.BottomRight:
                    BackgroundMapLayer.AddBookmarkListFrame(new Point(550, 200), orientation);
                    break;
            }
        }

        public void ShowConfigMenu(CornerMenu.CornerMenuLocation location)
        {
            switch (location)
            {
                case CornerMenu.CornerMenuLocation.TopLeft:
                    BackgroundMapLayer.ShowConfigPanel(new Point(350, 700), 90.0);
                    break;
                case CornerMenu.CornerMenuLocation.TopRight:
                    BackgroundMapLayer.ShowConfigPanel(new Point(350, 700), 90.0);
                    break;
                case CornerMenu.CornerMenuLocation.BottomLeft:
                    BackgroundMapLayer.ShowConfigPanel(new Point(350, 700), 270.0);
                    break;
                case CornerMenu.CornerMenuLocation.BottomRight:
                    BackgroundMapLayer.ShowConfigPanel(new Point(350, 700), 270.0);
                    break;
            }
        }

        public void showExitMenu(double orientation, CornerMenu.CornerMenuLocation location)
        {
            // FIX ME: fix the orientation of the exit confirmation dialog

            switch (location)
            {
                case CornerMenu.CornerMenuLocation.TopLeft:                    
                    //ExitDialogUpperLeft.RenderTransform = new RotateTransform(orientation);
                    ExitDialogUpperLeft.Visibility = Visibility.Visible;
                    break;
                case CornerMenu.CornerMenuLocation.TopRight:
                    //ExitDialogUpperRight.RenderTransform = new RotateTransform(orientation);
                    ExitDialogUpperRight.Visibility = Visibility.Visible;
                    break;
                case CornerMenu.CornerMenuLocation.BottomLeft:
                    //ExitDialogBottomLeft.RenderTransform = new RotateTransform(orientation);
                    ExitDialogBottomLeft.Visibility = Visibility.Visible;
                    break;
                case CornerMenu.CornerMenuLocation.BottomRight:                    
                    //ExitDialogBottomRight.RenderTransform = new RotateTransform(orientation);
                    ExitDialogBottomRight.Visibility = Visibility.Visible;
                    break;
            }
        }

        #endregion
        
    }
}